<template>
  <div class="loginBody">
    <div class="loginDiv">
      <div class="login-content">
        <h1 class="login-title">Login</h1>
        <el-form :model="loginForm" label-width="100px"
                 :rules="rules" ref="loginForm">
          <el-form-item label="Account" prop="no">
            <el-input style="width: 200px" type="text" v-model="loginForm.no"
                      autocomplete="off" size="small"></el-input>
          </el-form-item>
          <el-form-item label="Password" prop="password">
            <el-input style="width: 200px" type="password" v-model="loginForm.password"
                      show-password autocomplete="off" size="small" @keyup.enter.native="confirm"></el-input>
          </el-form-item>
          <el-form-item class="button-container">
                <el-button type="primary" @click="confirm" :disabled="confirm_disabled">confirm</el-button>
          </el-form-item>
          <div style="text-align: center">
            Forget Password？<a href="javascript:void(0)" style="text-decoration: none" @click="navReset">Retrieve password</a>
          </div>
          <el-form-item> 
            <el-link type="primary" @click="$router.push('/register')">Don't have an account? Click to register</el-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return{
                confirm_disabled:false,
                loginForm:{
                    no:'',
                    password:''
                },
                rules:{
                    no: [
                        { required: true, message: 'Please enter your account', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: 'Please enter password', trigger: 'blur' }
                    ],
                }
            }
        },
        methods:{

            confirm(){
                this.confirm_disabled=true;
                this.$refs.loginForm.validate((valid) => {
                    if (valid) { //valid成功为true，失败为false
                        //去后台验证用户名密码
                        this.$axios.post(this.$httpUrl+'/user/login',this.loginForm).then(res=>res.data).then(res=>{
                            console.log(res)
                            if(res.code==200){
                              //存储
                              sessionStorage.setItem("CurUser",JSON.stringify(res.data.user))
                              this.$store.commit("setMenu",res.data.menu)
                              //跳转到主页
                              this.$router.replace('/Home');
                            }else{
                                this.confirm_disabled=false;
                                alert('Verification failed, username or password incorrect！');
                                return false;
                            }
                        });
                    } else {
                        this.confirm_disabled=false;
                        console.log('Verification failed');
                        return false;
                    }
                });

            },
            navReset(){
                this.$router.push("/reset")
            }
        }
    }
</script>

<style scoped>
    .loginBody {
      position: absolute;
      width: 100%;
      height: 100%;
      background-color: #B3C0D1;
      background-image: url('../assets/images/login_bg.jpg'); /* 添加背景图 */
      background-size: cover; /* 背景图覆盖整个容器 */
      background-position: center; /* 背景图居中 */
      background-repeat: no-repeat; /* 不重复背景图 */
    }
    .loginDiv {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -200px;
        margin-left: -250px;
        width: 450px;
        height: 330px;
        background: #fff;
        border-radius: 5%;

    }
    .login-title {
        margin: 20px 0;
        text-align: center;
    }
    .login-content {
        width: 400px;
        height: 250px;
        position: absolute;
        top: 25px;
        left: 25px;
    }
</style>
